package sockets;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.HashMap;

import DB.ArrayOfChanges;


public class changesServerProtocol implements Runnable{
    BufferedReader in; 
    PrintWriter out;
    InetAddress address;
    public changesServerProtocol(BufferedReader in, PrintWriter out,InetAddress address) {
            this.in=in; this.out=out;
              this.address=address;
    }


    @Override
    public void run() {
    	
    	System.out.println("changesServerProtocol started");
            // TODO Auto-generated method stub
              String request=new String("");
              String response=new String("");
              //int changeID=9;
              String change = null, saveChange;              
              try 
              {
                      while(true)
                      {
   

                              
                              //changeID = in.read();
                              //System.out.println("changeID " +  changeID);
                              
                              //save the number
                              saveChange = change;
                              
                              change = in.readLine(); 
                              System.out.println("change " +  change);
                              
                              
                              //case it is anumber
                              if((!change.equals("createFile"))&&(change!=null)){
                            	  
                                  if(change.equals("0"))
                                  {
                                	  System.out.println("in if");
                                	  	  System.out.println("change = " + change);
                                          response=ArrayOfChanges.instance.lastChange();
                                          System.out.println("response - " + response);
                                  }
                                  //not a zero
                                  else
                                  {
                                	  System.out.println("in else");
                                	  	//string to int
                                	  int aInt = Integer.parseInt(change);
                                	  
                                      int changes_number=ArrayOfChanges.instance.lastIndex();
                                      response = ArrayOfChanges.instance.changeInIndex(changes_number-aInt);
                                      System.out.println("response - " + response); 
                                      
                                  }
                                  //write to client the last string form the array List 
                                  out.println(response);
                                  
                              }
                              //it is "createFile"
                              else{
                            	  System.out.println("if it's createFile");
                          	  	//string to int - the last one - saveChange!!!
                            	  int aInt = Integer.parseInt(saveChange);
                            	  
                                  int changes_number=ArrayOfChanges.instance.lastIndex();
                                  response = ArrayOfChanges.instance.changeInIndex(changes_number-aInt);
                            	  
                            	  String arr[]=response.split(" ");
                                  String filename=arr[1];
                                  new FileClient().sendFile(address, filename);
                              }
  
                       }
                      //end of while
              }
              catch (IOException e1) 
              {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
              }
              //end while
    }


}
